package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.bjgl.bjgl.business_web.entity.BCollege;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.business_web.mapper.BCollegeMapper;
import com.zhou.bjgl.bjgl.req.b_college.BCollegeQueryReq;
import com.zhou.bjgl.bjgl.resp.BCollegeResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.business_web.service.BCollegeSerive;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import com.zhou.bjgl.bjgl.util.RedisUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *学院servcie实现类
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@Service
public class BCollegeServiceImpl extends ServiceImpl<BCollegeMapper, BCollege> implements BCollegeSerive {

    @Resource
    private BCollegeMapper bCollegeMapper;

    @Resource
    private RedisUtil redisUtil;
    /**
     * 添加学院方法
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BCollegeQueryReq req){
        BCollege bCollege = CopyUtil.copy(req, BCollege.class);
        //查询学院是否重名
        QueryWrapper<BCollege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("college_name",req.getCollegeName());
        BCollege old = bCollegeMapper.selectOne(queryWrapper);
        if(old!=null){
            throw new BwException(BwExceptionEnum.NAME_EXISTED);
        }
        if (ObjectUtils.nullSafeEquals(req.getTypeForEdit(), "add")) {
            //进行插入
            bCollege.setDelStatus(0);
            int insert = bCollegeMapper.insert(bCollege);
            if (insert == 0) {
                throw new BwException(BwExceptionEnum.INSERT_FAIL);
            }
            redisUtil.del("collegeList");
        } else {
            //进行更新
            bCollegeMapper.updateById(bCollege);
        }
    }

    /**
     * 删除学院方法
     * @param req
     */
    @Override
    public void delete(BCollegeQueryReq req) {
        int i = bCollegeMapper.deleteById(req.getId());
        if (i == 0) {
            throw new BwException(BwExceptionEnum.DELETE_FAILID);
        }
    }

    /**
     * 分页获取学院列表
     * @param req
     * @return
     */
    @Override
    public PageResp<BCollegeResp> listCollege(BCollegeQueryReq req) {
        QueryWrapper<BCollege> CollegeWrapper = new QueryWrapper<>();
        //去除掉最顶层结构层级
        CollegeWrapper.notIn("id",1484744242220589057L);
        //进行分页查询
        PageHelper.startPage(req.getPage(),req.getSize());
        List<BCollege> bCollegeList = bCollegeMapper.selectList(CollegeWrapper);

        List<BCollegeResp> bCollegeResps = CopyUtil.copyList(bCollegeList, BCollegeResp.class);

        //构造返回结果
        PageInfo<BCollege> respPageInfo = new PageInfo<>(bCollegeList);
        PageResp<BCollegeResp> pageResp = new PageResp<>();
        pageResp.setPageList(bCollegeResps);
        pageResp.setTotal(respPageInfo.getTotal());

        return pageResp;
    }
}
